<template>
  <header>
    <div class="banxin tanxin header_box">
      <section>欢迎来到叩丁狼积分商城</section>
      <ul>
        <li class="avater_li">
          <img width="26" :src="userInfo.headeImg" alt="" />
          用户名 ：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li v-if="isLogin" class="header_btn cart_btn">
          <img src="../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <b>{{ cartTotal }}</b>
        </li>
        <li class="header_btn login_btn" v-else @click="goLogin">登录</li>
        <li class="header_btn login_btn" @click="outLogin">退出登录</li>
      </ul>
    </div>
  </header>
</template>

<script>
import { getUserInfoApi } from "@/request/api";
export default {
  data() {
    return {
      // 是否登录 true 为登录 false 代表默认未登录
      isLogin: false,
      // 用户信息
      userInfo: {
        // 用户头像
        headeImg: require("../assets/images/userImg.png"),
        // 用户名
        nickName: "游客",
        // 积分数
        coin: "--",
      },
      // 购物车商品的数量
      cartTotal: 0,
    };
  },
  methods: {
    goLogin() {
      // 显示登录的模态框 ，打开组件
      // 修改vuex当中的 isOpenLogin 的值 ， 触发 mutations 去打开 login 模态框
      this.$store.commit("changeIsLogin", true);
    },
    outLogin() {
      localStorage.removeItem("x-auth-token");
      this.$router.go(0);
    },
  },
  created() {
    getUserInfoApi().then((res) => {
      // console.log(res.data);
      if (res.data.code === 0) {
        let { userInfo, cartTotal } = res.data.data;
        this.userInfo = userInfo;
        this.isLogin = true;
        this.cartTotal = cartTotal;
      }
      console.log(this.userInfo);
    });
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
header {
  background: @black;
  height: 40px;
  color: #fffefe;
  font-size: 14px;
  .header_box {
    align-items: center;
    height: 40px;
    ul {
      display: flex;
      height: 40px;
      align-items: center;
      li {
        margin-right: 20px;
        cursor: pointer;
        &.avater_li {
          display: flex;
          height: 26px;
          align-items: center;
          img {
            border-radius: 50%;
            margin-right: 8px;
          }
        }
        &.header_btn {
          width: 124px;
          height: 40px;
          background: @blue;
        }
        &.cart_btn {
          display: flex;
          align-items: center;
          justify-content: center;
          span {
            margin: 0 8px;
          }
          b {
            width: 22px;
            height: 22px;
            background: @orange;
            border-radius: 50%;
            text-align: center;
            line-height: 22px;
          }
        }
        &.login_btn {
          text-align: center;
          line-height: 40px;
        }
      }
    }
  }
}
</style>